package com.cloud.code.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

/**
 * 描述:
 * java8流Test
 *
 * @author 54485
 * @create 2018-09-20 15:52
 */
public class StreamTest {

    private static List<People> peoples = new ArrayList<>();

    static {
        for(int i = 0;i<30;i++){
            int random = (int) (Math.random()*10);
            People people = new People();
            people.setUserName("liming"+random);
            people.setAge(random);
            people.setCreateDate(new Date());
            peoples.add(people);
        }
    }


    public static void main(String[] args) {

        //取出所有人年龄
//        List<Integer> integers = peoples.stream().map(People::getAge).collect(Collectors.toList());
//        System.out.println(integers);

        //去除所有人年龄去重
//        List<Integer> integers1 = peoples.stream().map(p -> p.getAge()).distinct().collect(Collectors.toList());

        //list中属性分别存入hashmap
//        Map<String, People> collect = peoples.stream().collect(Collectors.toMap(p -> p.getUserName(), p -> p));

        //当key重复时保留旧的还是新的。
//        Map<String, People> collect1 = peoples.stream().collect(Collectors.toMap(p -> p.getUserName(), u -> u, (oldV, newV) -> newV));

        //按照年龄大小排序
//        peoples.sort((a , b) -> b.getAge() - a.getAge());
//        List<Integer> integers2 = peoples.stream().map(People::getAge).collect(Collectors.toList());
//        System.out.println(integers2);



        //foreach
//        peoples.forEach((p) -> System.out.println(p.getAge()));
//        peoples.forEach(System.out::println);

        //按照年龄筛选去重
//        peoples.stream().filter(people -> people.getAge() > 8).distinct().forEach(System.out::println);
//
//        //reduce求和运算
//        Integer sum = peoples.stream().map(people -> people.getAge()).reduce(1, (p, item) -> p + item);
//        Integer sum2 = peoples.stream().map(people -> people.getAge()).reduce(1, Integer::sum);
//        System.out.println(sum + "|" + sum2);


        //排序,去重判断的是equals
//        peoples.stream().distinct().sorted((a,b) -> (a.getAge() - b.getAge() >0 ? 1: -1)).forEach(System.out::println);

        //limit保留3个
//        peoples.stream().limit(3).forEach(System.out::println);
        //skip或略前三个
//        peoples.stream().skip(3).forEach(System.out::println);

        //转换类型
//        Stream<String> stringStream = peoples.stream().map(p -> "年龄:" + p.getAge());
//        stringStream.forEach(System.out::println);

        //返回
//        Object[] objects = peoples.stream().toArray();
        //最小
//        People peopleMin = peoples.stream().min((a, b) -> a.getAge() - b.getAge()).get();
        //最大
//        People peopleMax = peoples.stream().min((a, b) -> b.getAge() - a.getAge()).get();
//        System.out.println(people.getAge());
        //返回流中数据总数
//        long count = peoples.stream().count();
        //返回第一个
//        People people = peoples.stream().findFirst().get();
//        People people1 = peoples.stream().findAny().get();
//        peoples.stream().collect(Collectors.toList()).forEach((p)-> System.out.print(p.getAge()+"-"));
//        System.out.println();
//        //可去重收集到set
//        peoples.stream().collect(Collectors.toSet()).forEach((p) -> System.out.print(p.getAge()+"-"));


        //合并两个数组
        IntStream stream = Arrays.stream(new int[]{1, 2, 3, 4, 4, 5, 5, 6, 7, 8});



    }


}
